﻿using System.Linq;
using System;
using System.Globalization;
using System.ServiceModel.DomainServices.Server;
using System.Reflection;
using System.Diagnostics;

namespace Arch.DomainServices
{
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
    public sealed class EntityDomainServiceDescriptionProviderAttribute : DomainServiceDescriptionProviderAttribute
    {
        public EntityDomainServiceDescriptionProviderAttribute(Type sampleType)
            : base(typeof(EntityDomainServiceDescriptionProvider))
        {
            ModelAssembly = sampleType.Assembly;
        }

        public Assembly ModelAssembly { get; private set; }

        public override DomainServiceDescriptionProvider CreateProvider(Type domainServiceType, DomainServiceDescriptionProvider parent)
        {
            if (domainServiceType == null)
            {
                throw new ArgumentNullException("domainServiceType");
            }

            if (!typeof(RepositoryDomainService).IsAssignableFrom(domainServiceType))
            {
                throw new ArgumentException(
                    String.Format(
                            CultureInfo.InvariantCulture,
                            "Service '{0}' must derive from '{1}",
                            domainServiceType.Name,
                            typeof(RepositoryDomainService).Name),
                    "domainServiceType");
            }

            return new EntityDomainServiceDescriptionProvider(domainServiceType, parent, ModelAssembly);
        }
    }
}
